{#**
*
* 功能描述（信息模板）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*#}
{#include file="header.tpl" title="title" active="cake" #}
{#include file="define.tpl" scope=parent#}
<div class="content middle">
    <div class="ui breadcrumb">
        <a class="section" href="index.php">主页</a>
        <i class="right"></i>
        <a class="section" href="index.php?action=cake.showlist">产品管理</a>
        <i class="right"></i>
        <span class="active section">产品价格</span>
    </div>
    <div class="search">
        <div class="searchTitle">
            产品信息
        </div>
        <div class="searchBar">
            <label>产品编号：</label>
            <label style="display: inline-block; margin-right: 50px;">{#$data['no']#}</label>
            <label>产品名称：</label>
            <label style="display: inline-block; margin-right: 50px;">{#$data['name']#}</label>
            <label>折扣：</label>
            <label style="display: inline-block; margin-right: 50px;">{#$data['discount']*10#}折</label>
        </div>
    </div>
    <div class="search">
        <div class="searchTitle">
            价格列表
        </div>
        <div class="searchBar" style="padding: 10px;">
        <table id='cakePrice' class="ui table segment">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>单价（￥）</th>
                    <th>规格</th>
                    <th>单位</th>
                    <th>描述</th>
                    <th>默认</th>
                    <th>修改</th>
                    <th>删除</th>
                </tr>
            </thead>
            <tbody>
                {#foreach $prices as $item#}
                    <tr>
                        <td>{#$item['id']#}</td>
                        <td>{#$item['price']#}</td>
                        <td>{#$item['size']#}</td>
                        <td>{#$item['unit']#}</td>
                        <td>{#$item['describe']#}</td>
                        <td>{#if $item['default'] == 1#}是{#else#}否{#/if#}</td>
                        <td><a href="#" onclick="mod_price(this);
                            return false">修改</a></td>
                        <td>
                            <a href="index.php?action=cake.delprice&cakeid={#$data['id']#}&id={#$item['id']#}" onclick="ajax_this(this);
                                return false">删除</a>
                        </td>
                    </tr>
                    {#$size_all[$item['size']] = 0#}
                {#/foreach#}
            </tbody>
        </table>
        </div>
    </div>
    <div class="search">
        <div class="searchTitle">
            添加价格
        </div>
        <div class="searchBar">
            <form class="ui form" id="form_price" action='index.php?action=cake.addmodprice&cakeid={#$data['id']#}' method="POST" onsubmit="return commit_price(this)">
                <div class="inputDIV">
                    <label class="title_one">ID：</label><input id="price_id" type='text' name='id' value='0' onchange="ch_price_btn($(this))" style="width: 60px;" />
                    <label class="title_one">单价（￥）：</label><input type='text' id="price_price" name='price' value='1' style="width: 60px;" />
                    <label class="title_one">规格：</label>
                    <select id = "price_size" name='size' style="width: 80px;">
                        {#foreach $size_all as $k => $v#}
                            <option value="{#$k#}">{#$k#}{#if $v==0#}-已存在{#/if#}</option>
                        {#/foreach#}
                    </select>
                    <label class="title_one">单位：</label><input id="price_unit" type='text' name='unit' value='磅' style="width: 60px;" />
                    <label class="title_one">描述：</label><input id="price_desp" type='text' name='describe' value='约13×13(cm) 454g' style="width: 200px;" />
                    <label class="title_one">默认：</label>
                    <select name="default" id="price_default">
                        <option value="0">否</option>
                        <option value="1">是</option>
                    </select>
                    <div class="button_bar" style="padding-right: 10px;">
                        <input id="price_btn" type="submit" value="添加" class="button align_right" />
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="button_bar">
        <input type="button" value="返回" class="button align_right" onclick="gotoUrl('index.php?action=cake.showlist');" />
    </div>
</div>
<script>
    function commit_price(obj)
    {
        if ($("#price_btn").val() == '添加' && $("#price_size").find("option:selected").text().length > 2)	//通过长度判断是否已存在
        {
            alert("选择的规格已经存在");
            return false;
        }
        if (ajax_commit(obj) == 0)
            location.reload();
        return false;
    }
    $('.ui.selection.dropdown').dropdown();
    function mod_price(obj)
    {
        try {
            obj = $(obj);
            var inputs = obj.parent().parent().children();
            inputs.each(function(idx) {
                switch (idx)
                {
                    case 0:
                        $("#price_id").val($(this).html());
                        break;
                    case 1:
                        $("#price_price").val($(this).html());
                        break;
                    case 2:
                        $("#price_size").val($(this).html());
                        break;
                    case 3:
                        $("#price_unit").val($(this).html());
                        break;
                    case 4:
                        $("#price_desp").val($(this).html());
                        break;
                    case 5:
                        if ($(this).html() == '是')
                            $("#price_default").val(1);
                        else
                            $("#price_default").val(0);
                        break;
                }
            });
            $("#price_btn").val("修改");
        } catch (e) {
            alert(e);
        }
    }

    function ch_price_btn(obj)
    {
        val = parseInt(obj.val());
        if (isNaN(val))
        {
            alert("输入非法");
            obj.val(0);
            return;
        }
        obj.val(val);
        if (val == 0)
            $("#price_btn").val("添加");
        else
            $("#price_btn").val("修改");
    }
</script>

{#include file="footer.tpl" #}
